﻿//编辑对话框
var dlg = new MyAppDialog(600, 540);

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

var cols = [[
    { field: "Id", title: 'Id', width: 10, hidden: true },
    { field: "ck", title: '', width: 20, checkbox: true },
    { field: "Sort", title: '顺序', sortable: true, width: 40, align: 'center' },
    { field: "Typecode", title: '字典分类', sortable: true, width: 80, align: 'left' },
    { field: "Code", title: '字典编码', sortable: true, width: 80, align: 'center' },
    { field: "Name_cn", title: '名称(简体)', sortable: true, width: 150, align: 'center' },
    { field: "Name_hk", title: '名称(繁体)', sortable: true, width: 80, align: 'center' },
    { field: "Name_en", title: '名称(英文)', sortable: true, width: 80, align: 'center' },
    { field: "Remark", title: '备注', sortable: true, width: 150, align: 'center' }
]];

$(document).ready(function () {
    //判断是否有编辑权限
    if (!canEdit) {
        $("#btndel").hide();
        $("#btnadd").hide();
    }
    //删除
    $("#btndel").click(function (evt) { evt.preventDefault(); del(); });
    //添加
    $("#btnadd").click(function (evt) {
        //var params = $("#datagrid").datagrid('options').queryParams;
        //params.s = $("#searchkey").val();
        //params.Dicttype = $("#Typecode").val();
        //$("#datagrid").datagrid("load");
        evt.preventDefault();
        dlg.setURL("新增字典", baseurl + "Manage/Dict/Edit/0?t=" + $("#Typecode").val() + "&" + Math.random());
        dlg.show();
    });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.Dicttype = $("#Typecode").val();
        $("#datagrid").datagrid("load");
    });
    
    $("#Typecode").change(function () {
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.Dicttype = $("#Typecode").val();
        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });

    LoadDatagrid();
});

//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '数据字典',
        url: baseurl + 'Manage/Dict/DictListJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            s: "",
            Dicttype: ""
        },
        sortName: '',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            if (!canEdit)
                dlg.setURL("查看字典目录", baseurl + "Manage/Dict/Edit/" + row.Id + "?" + Math.random());
            else
                dlg.setURL("编辑字典目录", baseurl + "Manage/Dict/Edit/" + row.Id + "?" + Math.random());

            dlg.show();
        },
        //onSelect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        //onUnselect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //}
    });
    //PaginationInit();
}

function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    if (confirm(csms_lang.message_delete_confrim)) {
        var ids = "";
        for (var i = 0; i < row.length; i++) {
            ids += row[i].Id + ",";
        }

        $.ajax({
            type: "post",
            url: baseurl + "Manage/Dict/Delete",
            data: { ids: ids },
            success: function (e) {
                if (e.Ret == "0") {
                    $("#datagrid").datagrid("reload");
                } else {
                    $.messager.alert(csms_lang.message_error, e.Msg);
                }
            }
        });
    }
}
